package com.sanyang.pda.adapter;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sanyang.pda.bean.PalletBean;
import com.sanyang.pda.event.EventDate;
import com.sanyang.pda.utils.StringConfig;
import com.sanyang.pda.view.datepicker.CustomDatePicker;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.ui.mvp.base.adapter.BaseAdapter;
import com.wangshen.base.ui.mvp.base.view.BaseView;
import com.wangshen.projectbase.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class PutOnAdapter extends BaseAdapter<PalletBean> {
    private CustomDatePicker mDatePicker;
    private String entryNo;
    BaseView baseView;
    public PutOnAdapter(int layoutResId, @Nullable List<PalletBean> data) {
        super(layoutResId, data);
        EventBus.getDefault().register(this);
    }

    public void setEntryNo(String entryNo, BaseView baseView) {
        this.entryNo = entryNo;
        this.baseView=baseView;
    }

    @Override
    protected void convert(BaseViewHolder helper, PalletBean item) {
        super.convert(helper, item);
        helper.setText(R.id.tv_name,item.getArticleName());
        helper.setText(R.id.tv_good_spec,item.getSpec());
        helper.setText(R.id.tv_batch,item.getBatchNo());
        helper.setText(R.id.tv_inventory_quantity,item.getInNum());
        helper.setText(R.id.et_product_time,item.getProductTime());
        helper.setText(R.id.et_admire_date,item.getExpiringTime());
        EditText tv_batch=helper.getView(R.id.tv_batch);
        tv_batch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setBatchNo(tv_batch.getText().toString());
            }
        });
        EditText tv_inventory_quantity=helper.getView(R.id.tv_inventory_quantity);
        tv_batch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setInNum(tv_inventory_quantity.getText().toString());
            }
        });
        TextView et_admire_date=helper.getView(R.id.et_admire_date);
        et_admire_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle=new Bundle();
                bundle.putString(StringConfig.ENTRYNO,entryNo);
                bundle.putString(StringConfig.ID,item.getArticleId());
                bundle.putInt(StringConfig.TYPE,20);
                bundle.putInt(StringConfig.INDEX,helper.getAdapterPosition());
                ARouter.getInstance().build(AppActivityKey.CALENDARACTIVITY).with(bundle).navigation(getContext());
               /* DateSelectUtils.getInstance().showSelect(baseView, entryNo, item.getArticleId(), 20, new DateSelectUtils.OnSelectCallBack() {
                    @Override
                    public void onSelect(String content) {
                        et_admire_date.setText(content);
                        item.setExpiringTime(content);
                    }
                });*/
                /*long beginTimestamp = DateFormatUtils.str2Long("2000-01-01", false);
                long endTimestamp = DateFormatUtils.str2Long("2500-01-01", false);
                // 通过时间戳初始化日期，毫秒级别
                mDatePicker = new CustomDatePicker(getContext(), new CustomDatePicker.Callback() {
                    @Override
                    public void onTimeSelected(long timestamp, TextView v) {
                        v.setText(DateFormatUtils.long2Str(timestamp, false));
                        item.setExpiringTime(DateFormatUtils.long2Str(timestamp, false));
                    }
                }, beginTimestamp, endTimestamp);
                mDatePicker.show(System.currentTimeMillis(),et_admire_date);*/
            }
        });
        TextView et_product_time=helper.getView(R.id.et_product_time);
        et_product_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle=new Bundle();
                bundle.putString(StringConfig.ENTRYNO,entryNo);
                bundle.putString(StringConfig.ID,item.getArticleId());
                bundle.putInt(StringConfig.TYPE,10);
                bundle.putInt(StringConfig.INDEX,helper.getAdapterPosition());
                ARouter.getInstance().build(AppActivityKey.CALENDARACTIVITY).with(bundle).navigation(getContext());
               /* DateSelectUtils.getInstance().showSelect(baseView, entryNo, item.getArticleId(), 10, new DateSelectUtils.OnSelectCallBack() {
                    @Override
                    public void onSelect(String content) {
                        et_admire_date.setText(content);
                        item.setExpiringTime(content);
                    }
                });*/
                /*long beginTimestamp = DateFormatUtils.str2Long("2000-01-01", false);
                long endTimestamp = DateFormatUtils.str2Long("2500-01-01", false);
                // 通过时间戳初始化日期，毫秒级别
                mDatePicker = new CustomDatePicker(getContext(), new CustomDatePicker.Callback() {
                    @Override
                    public void onTimeSelected(long timestamp, TextView v) {
                        v.setText(DateFormatUtils.long2Str(timestamp, false));
                        item.setProductTime(DateFormatUtils.long2Str(timestamp, false));
                    }
                }, beginTimestamp, endTimestamp);
                mDatePicker.show(System.currentTimeMillis(),et_product_time);*/
            }
        });
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventDate(EventDate eventDate){
        if (eventDate.getType() == 10){
           getData().get(eventDate.getIndex()).setProductTime(eventDate.getDate());
        }else {
            getData().get(eventDate.getIndex()).setExpiringTime(eventDate.getDate());
        }
        notifyItemChanged(eventDate.getIndex());
    }
}
